

void set_period(){     
  if(func_index==4||func_index==5){  
    if((digitalRead (NS)==0)||(digitalRead (SF)==0)){
      delay(20);
      if((digitalRead (NS)==0)){
        digitalWrite(DIR, !digitalRead(DIR)); // 方向切换
        UI_refresh();
        while(!digitalRead (NS));
      }
      if((digitalRead (SF)==0)){
        speedtabi++;
        if(speedtabi>3) speedtabi=0;
        Timer1.setPeriod(EQ_speedtab[speedtabi]); 
        UI_refresh(); 
        while(!digitalRead (SF));
      }
    } 
  }
  if(func_index==6){ //处于云台模式的页面
    if((digitalRead (NS)==0)){
      delay(20);
      if((digitalRead (NS)==0)){
        digitalWrite(DIR, !digitalRead(DIR)); 
        UI_refresh();
        while(!digitalRead (NS));
      }
    }
  }
   
}


void motorstep() { 

  if(func_index==4||func_index==5||(func_index>=19&&func_index<=23)||func_index==30){  
    digitalWrite(STEP, !digitalRead(STEP)); // step
  }
  if((func_index==24)&&(if_pulseend == false)){  
    if(pulseCount<angle_pulsen){
      digitalWrite(STEP, !digitalRead(STEP)); 
      pulseCount++;
    }
    else {
      if_pulseend = true;
      angle_p = 100;
      pulseCount = 0;
      delay(20);//稳定时间
    }
  }
  if((func_index>=12&&func_index<=14)&&(outputEnabled == true)){ 
    digitalWrite(STEP, !digitalRead(STEP));
  }
}


void shutter_EQ_run(){
  if(func_index==30||func_index==31){  
    unsigned long startTime = millis();
    ifex = true;
    digitalWrite(shutter, HIGH);
    digitalWrite(focus, HIGH);
    EEPROM.get(Shutter_num_eeprom, Shutter_n);
    UI_refresh();
    for(Shutter_n;Shutter_n>=0;){
      if ((digitalRead (OK)==0)|| Shutter_n==0 ){
        break;
      }
      if ((millis() - startTime > 1000)&&(ifex == true)) {
        Ex_time--;
        startTime = millis();
        UI_refresh();
        }
      if ((millis() - startTime > 1000)&&(ifex == false)) {
        Interval_time--;
        startTime = millis();
        UI_refresh();
        } 
      if ((Ex_time==0)&&(Shutter_n>0)) {
        digitalWrite(shutter, LOW);
        digitalWrite(focus, LOW);
        ifex = false;
        EEPROM.get(Ex_time_eeprom, Ex_time);
        Shutter_n--;
        UI_refresh();
        }
      if ((Interval_time==0)&&(Shutter_n>0)) {
        digitalWrite(shutter, HIGH);
        digitalWrite(focus, HIGH);
        ifex = true;
        EEPROM.get(Interval_time_eeprom, Interval_time);
        UI_refresh();
        }
      }
      EEPROM.get(Interval_time_eeprom, Interval_time);
      EEPROM.get(Ex_time_eeprom, Ex_time);
      digitalWrite(shutter, LOW);
      digitalWrite(focus, LOW);
      ifex = false;
      if(func_index ==30) func_index = 4;  
      else func_index = 13;
      current_operation_index = table[func_index].current_operation;
      UI_refresh();
  }
}


void shutter_Gimbal_run(){
  if(func_index==24){ 
    pulseCount = 0;
    angle_pulsen = Rota_angle*(float)Dece_ratio*(float)Motor_total_step/360*32; 
    period_Gimbal = (float)Interval_time*1000/(float)angle_pulsen*1000/2; 
    if(period_Gimbal<=MAX_us) period_Gimbal = MAX_us;
    Timer1.setPeriod(period_Gimbal); // 调整定时器周期
    //Interval_time = angle_pulsen*period_Gimbal/1000000*2;     
    unsigned long currentTime;       
    unsigned long startTime_forUI;   //
    unsigned long elapsedTime;
    uint16_t Ex_time_bp = Ex_time*1000;
    EEPROM.get(Shutter_num_eeprom, Shutter_n);
    UI_refresh();
    currentTime =  millis();
    for(Shutter_n;Shutter_n>=0;){
      if ((digitalRead (OK)==0) || Shutter_n==0 ){ 
        break;
      }
      if(if_pulseend == true){  //当电机没在运行
          UI_refresh();
          digitalWrite(focus, HIGH);             
          elapsedTime = millis() - currentTime;  
        if ((elapsedTime >= focustime)) {        
          digitalWrite(shutter, HIGH);           
          ifex = true;
        }
        if ((elapsedTime >= Ex_time_bp + focustime)) { 
          digitalWrite(focus, LOW);
          digitalWrite(shutter, LOW);
          ifex = false;
          currentTime = millis();
          startTime_forUI = currentTime;
          EEPROM.get(Ex_time_eeprom, Ex_time);
          Shutter_n--;
          UI_refresh();
          delay(20);//稳定时间
          if(Shutter_n>0) if_pulseend = false;
        }
      }
      if ((millis() - startTime_forUI > 1000)&&(ifex == true)) { 
        Ex_time--;
        startTime_forUI = millis();
        UI_refresh();
        }
      if(if_pulseend == false){       
        currentTime = millis();
        startTime_forUI = currentTime;
        angle_p = (float)pulseCount/(float)angle_pulsen*100;
        UI_refresh();
      }
    }
    if_pulseend = true;
    pulseCount = angle_pulsen; 
    Timer1.setPeriod(EQ_speedtab[speedtabi]);
    EEPROM.get(Ex_time_eeprom, Ex_time);
    digitalWrite(shutter, LOW);
    digitalWrite(focus, LOW);
    ifex = false;
    func_index = 6; 
    current_operation_index = table[func_index].current_operation;
    UI_refresh();
  }
}

//电调模式
void focuser_run(){
  if(func_index==12){
    if((digitalRead (LEFT)==0)||(digitalRead (RIGHT)==0)||(digitalRead (SF)==0))
    {
      delay(20);//消抖
      if((digitalRead (SF)==0)){
        speedtabi_2++;
        if(speedtabi_2>3) speedtabi_2 = 0;
        Timer1.setPeriod(FC_speedtab[speedtabi_2]); 
        UI_refresh(); 
        while(!digitalRead (SF));
      }
      if((digitalRead (LEFT)==0) )
      {
        digitalWrite(DIR,LOW);
        outputEnabled = true;
        cursor_p = 32;       
        UI_refresh();
        unsigned long cur_time = millis();
        while(!digitalRead (LEFT)){
          if (millis()-cur_time>(50)){
            cursor_p=cursor_p-8;
            if(cursor_p<=0) cursor_p = 32; 
            UI_refresh(); 
            cur_time = millis();
          }
        }
      }
      if((digitalRead (RIGHT)==0) )
      {
        digitalWrite(DIR,HIGH);
        outputEnabled = true;
        cursor_p = 96;       
        UI_refresh();
        unsigned long cur_time = millis();
        while(!digitalRead (RIGHT)){
          if (millis()-cur_time>(50)){
            cursor_p=cursor_p+8;
            if(cursor_p>=128) cursor_p = 96; 
            UI_refresh();
            cur_time = millis();
          }
        }
      }
      outputEnabled = false;
      cursor_p = 128;
      UI_refresh();
    }
    if((digitalRead (OK)==0)){  
      delay(10);//
      digitalWrite(focus,HIGH);
      digitalWrite(shutter,HIGH);
      unsigned long cur_time = millis();
      while(!digitalRead (OK)){
        if (millis()-cur_time>1000){
          extime_fc++;
          UI_refresh();
          cur_time = millis();
        }
      }
      digitalWrite(focus,LOW);
      digitalWrite(shutter,LOW);
      extime_fc =0;
      UI_refresh();
    }
    Timer1.setPeriod(EQ_speedtab[speedtabi]);
  }
}
  
